<?php
namespace app\admin\controller;

use app\admin\logic\GoodsLogic;
use app\admin\model\AuthGroupAccess;
use app\BaseController;
use think\Exception;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Users extends BaseController{
    //user  用户列表
    public function user(){
    $list=\app\admin\model\Users::userList();
    $page=$list->render();
    View::assign(['list'=>$list,'page'=>$page]);
    return View::fetch();
    }
    //user  用户添加
    public function userAdd(){
        if (Request::isPost()){
            $post=Request::post();
            list($status, $paths) = Upload::imgUpload('logo');
            if ($status) {
                $post['logo'] = $paths;
            }
            $post['add_time']=date('Y-m-d',time());
            $post['upwd']=md5('123456');
            list($res,$access)=\app\admin\model\Users::userAdd($post);
            if ($res && $access){
                $this->success('添加成功',url('user'));
            }else{
                $this->error('添加失败');
            }
        }else{
            $auth=\app\admin\model\Users::userAdd([]);
            View::assign('auth',$auth);
            return View::fetch();
        }
    }
    //user  用户修改
    public function userEdit(){
    if (Request::isPost()){
        $param=Request::param();
        list($status, $logo) = Upload::imgUpload('logo');
        if (!$status){
            $this->error($logo);
        }
        if ($status && !empty($logo)) {
            list($path_date, $path_dir)=GoodsLogic::setImgUrl($logo,'users/');
            $param['logo']=$path_dir;
        }
        $obj=\app\admin\model\Users::find($param['id']);
        $old= $obj->logo;
        $res=\app\admin\model\Users::userEdit($param);
        if ($res){
            //单图移动临时目录到正式目录
            if ($status && !empty($logo)) {
                GoodsLogic::setImgDir($logo, $path_date, $path_dir, './images/');
            }
            //删除单图老图路径
            if ($status && !empty($logo) &&!empty($old)){unlink('./images/'.$old);}
            $this->success('修改成功',url('user'));
        }else{
            $this->error('修改失败');
        }

    }else{
        $param=Request::param();
        list($find,$auth)=\app\admin\model\Users::userEdit($param);
        View::assign($find);
        View::assign('auths',$auth);
        return View::fetch();
    }



    }
    //user 用户删除
     public function userDel(){
        $get=Request::get();
        list($res,$access)=\app\admin\model\Users::userDel($get);
        if ($res && $access){
            $this->success('删除成功',url('user'));
        }else{
            $this->error('删除失败');
        }
      }
}